﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

using ScrumTable.Common.Properties;

namespace ScrumTable.Common.Logging
{
    #region Usings

    using System;
    using log4net;

    #endregion

    /// <summary>
    /// Contains the a time base logging functionality. You should use
    /// this class in conjunction with the using() method.
    /// </summary>
    /// <typeparam name="T">Specifies the logging category.</typeparam>
    public class TimeLogger<T> : IDisposable
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        
        private readonly DateTime _startTime;
        private readonly string _sectionName;
        private readonly bool _debugOnly = false;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new TimeLogger instance.
        /// </summary>
        /// <param name="sectionName">Specifies the log section name.</param>
        public TimeLogger(string sectionName)
        {
            if (IsLoggingEnabled())
            {
                _startTime = DateTime.Now;
                _sectionName = (sectionName ?? string.Empty);
            }
        }

        /// <summary>
        /// Creates a new TimeLogger instance.
        /// </summary>
        /// <param name="debugOnly">Specifies if the time logger is for tracing purposes only.</param>
        /// <param name="sectionNameFormat">Specifies the format string which is used to
        /// combine with the args. The format string and args will result in the log section name.</param>
        /// <param name="args">Arguments which should be inserted into the format string.</param>
        public TimeLogger(bool debugOnly, string sectionNameFormat, params object[] args)
        {
            _debugOnly = debugOnly;

            if (IsLoggingEnabled())
            {
                _startTime = DateTime.Now;
                _sectionName = string.Format(sectionNameFormat ?? string.Empty, args);
            }
        }

        /// <summary>
        /// Creates a new TimeLogger instance.
        /// </summary>
        /// <param name="sectionNameFormat">Specifies the format string which is used to
        /// combine with the args. The format string and args will result in the log section name.</param>
        /// <param name="args">Arguments which should be inserted into the format string.</param>
        public TimeLogger(string sectionNameFormat, params object[] args)
            : this(false, sectionNameFormat, args)
        {
        }

        /// <summary>
        /// Creates a new TimeLogger instance.
        /// </summary>
        public TimeLogger()
            : this(string.Empty)
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Performs application-defined tasks associated with freeing, 
        /// eleasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (IsLoggingEnabled())
            {
                TimeSpan elapsedTime = DateTime.Now - _startTime;
                Log(Resources.LogTimeMeasure, _sectionName, Convert.ToInt32(Math.Round(elapsedTime.TotalMilliseconds)));
            }
        }

        private void Log(string format, params object[] args)
        {
            if (_debugOnly)
                Logger<T>.DebugFormat(format, args);
            else
                Logger<T>.InfoFormat(format, args);
        }

        private bool IsLoggingEnabled()
        {
            if (_debugOnly)
            {
#if DEBUG || SimulatorDebug
                return Logger<T>.IsDebugEnabled;
#else
                return false;
#endif
            }
            else
            {
                return Logger<T>.IsInfoEnabled;
            }
        }

        #endregion
    }
}
